module view {
    //宠物室	
    export class PetHouseView extends eui.Component {
        closeBtn: eui.Button
        descGroup: eui.Group
        petsGroup: eui.Group
        petNameLabel: eui.Label
        petInfoLabel: eui.Label
        progressBar: eui.ProgressBar
        trainBtn: eui.Button
        hatchBtn: eui.Button
        selectPet: any
        battlePetsGroup: eui.Component
        petCons: any[]
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = 'resource/skin/PetHouseSkin.exml'
            this.name = 'PetHouseView'           
        }

        private onComplete(): void {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.hatchBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.showPetEgg,this)
            this.progressBar.addEventListener(egret.TouchEvent.TOUCH_TAP,this.workbenchTouch,this)
            this.showPets()
            this.showWorkbench()
        }

        private showPetEgg(): void {
            var petLayer = new PetLayer(this)
            this.addChild(petLayer)
        }

        public showWorkbench() {
            var workBenchPet = gplayer.workbenches.pet[0]
            var p = new HatchPetEggProgressBar(this.progressBar,workBenchPet)
        }

        private workbenchTouch(evt: egret.TouchEvent): void {
            var workbench = gplayer.workbenches.pet[0]
            if(!workbench) {
                return
            }
            var petEggId = workbench.id
            var completeTime = workbench.completeTime
            var petId = totalConfig[petEggId].getPet
            var nowTime = new Date().getTime()
            if(completeTime <= nowTime) {
                var pet = new game.Pet({
                    id: petId,
                    lvl: 1
                })
                gplayer.petHouse.push(pet)
                gplayer.workbenches.pet.splice(0,1)
                this.showWorkbench()
                this.showPets()
            }
        }

        private close(): void {
            this.parent.removeChild(this)
        }

        private showPets(): void {
            this.petsGroup.removeChildren()
            this.battlePetsGroup.removeChildren()
            var firstFlag = true
            this.petCons = []
            for(var i in gplayer.battlePets) {
                var pet = gplayer.battlePets[i]
                var selectButton: eui.RadioButton = new eui.RadioButton();
                selectButton.skinName = 'resource/control/PetRadioBtnSkin.exml'
                selectButton.label = templateName[pet.id]
                selectButton.groupName = "petsGroup"
                selectButton.horizontalCenter = 0  
                this.battlePetsGroup.addChild(selectButton)
                selectButton.y += i * 70
                selectButton['originalY'] = i * 70
                selectButton.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.petBEGIN,this)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.petMove,this)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_END,this.petEND,this)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.petEND,this)
                selectButton.addEventListener(eui.UIEvent.CHANGE,this.petTouch,this)
                this.petCons[i] = selectButton
                var petInfo = {
                    index: i,
                    flag: 1
                }
                selectButton.value = petInfo
                if(firstFlag) {
                    firstFlag = false
                    this.selectPet = petInfo
                    selectButton.selected = true
                    this.showPetInfo(petInfo)
                }
            }
            for(var i in gplayer.petHouse) {
                var pet = gplayer.petHouse[i]
                var selectButton: eui.RadioButton = new eui.RadioButton();
                selectButton.skinName = 'resource/control/PetRadioBtnSkin.exml'
                selectButton.label = templateName[pet.id]
                selectButton.groupName = "petsGroup"
                selectButton.horizontalCenter = 0  
                this.petsGroup.addChild(selectButton)    
                var petInfo = {
                    index: i,
                    flag: 2
                }
                selectButton.value = petInfo
                selectButton.addEventListener(eui.UIEvent.CHANGE,this.petTouch,this)
                if(firstFlag) {
                    firstFlag = false
                    this.selectPet = petInfo
                    selectButton.selected = true
                    this.showPetInfo(petInfo)
                }
            }
            this.trainBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.petLvl,this)
            this.petNameLabel.addEventListener(egret.TouchEvent.TOUCH_TAP,this.petBattle,this)
        }
        private petBEGIN(evt: egret.TouchEvent): void {
            var target = evt.target
            target['flag'] = true
            target['startPos'] = {
                x: evt.localX,
                y: evt.localY
            }
            evt.stopPropagation()
            target.parent.swapChildrenWithEnd(target)
        }
        private petMove(evt: egret.TouchEvent): void {
            var target = evt.target
            if(target['flag'] !== true) {
                return
            }
            var startPos = target['startPos']
            var relativeX = evt.localX - startPos.x
            var relativeY = evt.localY - startPos.y
            target.y += relativeY
        }

        private petEND(evt: egret.TouchEvent): void {
            var target = evt.target
            target['flag'] = false
            var y = target.y
            for(var i in this.petCons) {
                if(y < this.petCons[i]['originalY'] + 25 && y > this.petCons[i]['originalY'] - 25 && this.petCons[i] !== target) {
                    var j = i
                    var k = target.value.index
                    game.playerHandle.petTransPos(gplayer,j,k)
                    this.showPets()
                    return
                }
            }
            target.y = target['originalY']
        }

        private petTouch(evt: eui.UIEvent): void {
            var target = evt.target
            var petInfo = target.value
            this.showPetInfo(petInfo)
        }

        private showPetInfo(petInfo): void {
            this.selectPet = petInfo
            var pet
            if(petInfo.flag === 1) {
                pet = gplayer.battlePets[petInfo.index]
            } else {
                pet = gplayer.petHouse[petInfo.index]
            }
            this.petNameLabel.text = templateName[pet.id]
            this.petInfoLabel.text = '等级' + pet.lvl
        }

        private petBattle(): void {
            var petInfo = this.selectPet 
            if(petInfo.flag === 1) {
                game.playerHandle.petBattle(gplayer,petInfo.index)
            } else {
                game.playerHandle.petRest(gplayer,petInfo.index)
            }
            this.showPets()
        }

        private petLvl(): void {
            game.playerHandle.lvlup(this.selectPet)
            this.showPetInfo(this.selectPet)
        }
    }

    class HatchPetEggProgressBar {
        progressBar: eui.ProgressBar
        timer: egret.Timer
        workBenchPet: any
        itemId: number
        completeTime: number
        duration: number
        public constructor(progressBar: eui.ProgressBar,workBenchPet) {
            this.progressBar = progressBar
            this.progressBar.maximum = 1000
            this.progressBar.labelFunction = this.barLabelFunction
            if(!workBenchPet) {
                this.progressBar.slideDuration = 0
                this.progressBar.value = 0
                return
            }
            this.workBenchPet = workBenchPet
            this.itemId = this.workBenchPet.id
            this.completeTime = this.workBenchPet.completeTime
            this.duration = totalConfig[this.itemId].duration
            this.progressBar['getPetId'] = totalConfig[this.itemId].getPet
            var nowTime = new Date().getTime()
            var per = 1000 - (this.completeTime - nowTime) / this.duration * 1000
            this.progressBar.value = per
            this.timer = new egret.Timer(200,(this.completeTime - nowTime) / 200 + 1)
            this.timer.addEventListener(egret.TimerEvent.TIMER,this.timerHandler,this)
            this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this)
            this.timer.start()
        }

        private barLabelFunction(value: number,maximum: number): string {
            if(value === 0) {
                return '孵化台'
            } else if(Math.ceil(value / maximum * 100) === 100) {
                return templateName[this['getPetId']]
            } else {
                return '孵化中... ' + Math.ceil(value / maximum * 100) + '%'
            }
        }

        private timerHandler(evt: egret.TimerEvent): void {
            var nowTime = new Date().getTime()
            if(this.completeTime < nowTime) {
                this.progressBar.value = 1000
                return
            }
            var per = 100 - (this.completeTime - nowTime) / this.duration * 100
            var cd = (this.completeTime - nowTime) / 1000
            this.progressBar.value = per * 10
        }

        private timerComFunc(event: egret.TimerEvent) {
        }
    }

    class PetLayer extends eui.Component {
        cancelBtn: eui.Button
        confirmBtn: eui.Button
        petEggGroup: eui.Button
        parentView: PetHouseView
        hatchPet: any
        public constructor(parentView) {
            super()
            this.parentView = parentView
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/PetLayerSkin.exml"
            this.hatchPet = {
                packageType: 0,
                index: 0,
            }
        }

        private onComplete(): void {
            this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.confirmBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.hatchPetEgg,this)
            this.showEgg()

            var outline: egret.Shape = new egret.Shape;
            outline.graphics.lineStyle(3,0x00ff00)
            outline.graphics.beginFill(0x000000,0)
            outline.graphics.drawRect(0,0,400,400)
            outline.graphics.endFill()
            this.petEggGroup.addChild(outline)
        }

        private close(): void {
            console.log(this.parent)
            this.parent.removeChild(this)
        }

        private showEgg(): void {
            for(var i in gplayer.bag.items) {
                var item = gplayer.bag.items[i]
                if(item && game.utilsHandle.getItemType(item.id) === 13) {
                    var selectButton: eui.RadioButton = new eui.RadioButton()
                    selectButton.skinName = 'resource/control/PetRadioBtnSkin.exml'
                    selectButton.label = templateName[item.id]
                    selectButton.groupName = "petEggGroup"
                    selectButton.horizontalCenter = 0  
                    this.petEggGroup.addChild(selectButton)
                    selectButton['hatchPet'] = {
                        index: i,
                        packageType: gplayer.bag.type
                    }
                    selectButton.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectPetEgg,this)
                }
            }

        }

        private selectPetEgg(evt: egret.TouchEvent): void {
            var target = evt.target
            this.hatchPet = target['hatchPet']
        }

        protected hatchPetEgg(evt: egret.TouchEvent): void {
            var pack: game.Package
            if(this.hatchPet.packageType === gplayer.bag.type) {
                pack = gplayer.bag
            } else if(this.hatchPet.packageType === gplayer.store.type) {
                pack = gplayer.store
            } else {
                this.parent.removeChild(this)
                return
            }
            var item = pack.items[this.hatchPet.index]
            var petEggConfig = templateConfig.petEgg[item.id]
            if(gplayer.workbenches.pet.length >= 1) {
                return
            }

            var completeTime = new Date().getTime() + petEggConfig.duration
            var petEgg = {
                id: item.id,
                completeTime: completeTime
            }
            gplayer.workbenches.pet.push(petEgg)
            pack.removeItemByPos(this.hatchPet.index)
            this.parentView.showWorkbench()
            this.parent.removeChild(this)
        }
    }


}
